
package com.jf.cloud.order.service.impl;

import com.jf.cloud.api.delivery.dto.DeliveryOrderItemDTO;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.util.PageUtil;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.order.constant.DeliveryType;
import com.jf.cloud.order.mapper.PurchaseOrderItemMapper;
import com.jf.cloud.order.model.PurchaseOrderItem;
import com.jf.cloud.order.service.PurchaseOrderItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 采购商品
 *
 * @author zz
 * @date 2022-06-22 17:12:02
 */
@Service
public class PurchaseOrderItemServiceImpl implements PurchaseOrderItemService {

    @Autowired
    private PurchaseOrderItemMapper purchaseOrderItemMapper;

    @Override
    public PageVO<PurchaseOrderItem> page(PageDTO pageDTO) {
        return PageUtil.doPage(pageDTO, () -> purchaseOrderItemMapper.list());
    }

    @Override
    public PurchaseOrderItem getByPurchaseSpuId(Long purchaseSpuId) {
        return purchaseOrderItemMapper.getByPurchaseSpuId(purchaseSpuId);
    }

    @Override
    public void save(PurchaseOrderItem purchaseOrderItem) {
        purchaseOrderItemMapper.save(purchaseOrderItem);
    }


    @Override
    public void updateByDeliveries(List<DeliveryOrderItemDTO> deliveryOrderItems, Integer deliveryType) {
        int devType = 0;
        for (DeliveryOrderItemDTO deliveryOrderItem : deliveryOrderItems) {
            devType = purchaseOrderItemMapper.getDevTypeByOrderItemId(deliveryOrderItem.getOrderItemId());
            if (Objects.equals(devType, DeliveryType.DELIVERY.value())){
                //如果快递项已有快递类型，则set进去应当是快递类型
                deliveryType = devType;
            }
            purchaseOrderItemMapper.updateByDelivery(deliveryOrderItem,deliveryType);
        }
    }

    @Override
    public int countUnDeliveryNumByOrderId(Long orderId) {
        return purchaseOrderItemMapper.countUnDeliveryNumByOrderId(orderId);
    }

    @Override
    public boolean getDevTypeByOrderId(Long orderId) {
        List<Integer> devTypes = purchaseOrderItemMapper.getDevTypeByOrderId(orderId);
        return devTypes.contains(DeliveryType.DELIVERY.value());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateBatchById(List<PurchaseOrderItem> purchaseOrderItemListDb) {
        for (PurchaseOrderItem purchaseOrderItem : purchaseOrderItemListDb) {
            purchaseOrderItemMapper.update(purchaseOrderItem);
        }
//        purchaseOrderItemMapper.updateBatchById(purchaseOrderItemListDb);
    }

    @Override
    public void updateStatusByPurchaseOrderId(Integer purchaseOrderStatus, Long purchaseOrderId) {
        purchaseOrderItemMapper.updateStatusByPurchaseOrderId(Collections.singletonList(purchaseOrderId), purchaseOrderStatus);
    }
}
